package client.comm.baoding.fragment

import android.view.View
import client.comm.baoding.R
import client.comm.baoding.adapter.JifenAdapter
import client.comm.baoding.api.bean.IntegralRet
import client.comm.baoding.databinding.FragmentLoadmorepageBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.vm.JifenViewModel
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.widget.EmptyLayout

class JifenOneFragment : BaseFragment<FragmentLoadmorepageBinding, JifenViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_loadmorepage
    }

    val adapter: JifenAdapter by lazy {
        JifenAdapter(requireContext(), mImgLoader).apply {
            enterConvert = {
                MsgTitleDialog(requireContext()).apply {
                    title = "是否确认转换？"
                    ok = {
                        viewModel.integralConvertCall(it.batch_id)

                    }
                    okTxt = "确认"
                    cancleTxt = "取消"
                }.show()
            }
        }
    }

    override fun initViewModel(): JifenViewModel = createViewModel()

    override fun initWidget() {

        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))

        binding.recyclerView.adapter = adapter
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdOne()
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true

            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.batch_list as MutableList<IntegralRet.Integral?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }


        })
    }

//    override fun onResume() {
//        super.onResume()
//    }

    override fun initData() {
        adapter.hasMoreState.value = true
        viewModel.pageOne.value = 0
        viewModel.holdOne()

//        //以下为测试数据
//        val mList2: ArrayList<HmjlRet.Coin> = arrayListOf()
//        mList2.add(HmjlRet.Coin("", "", 0, 0.0, 0.0, 0, 0.0, 0))
//        mList2.add(HmjlRet.Coin("", "", 0, 0.0, 0.0, 0, 0.0, 0))
//
//        adapter.submitList(mList2)
////        adapter.appendList(mList2)
//        binding.emptyLayout.visibility = View.GONE
//        adapter.hasMoreState.value = false


    }


}